﻿using System.Configuration;
using System.IO;
using System.Text;
using System.Web;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
                                
namespace TWA.TFS
{
    /// <summary>
    /// Tools for transforming XML.
    /// </summary>
    internal static class Transform
    {
        /// <summary>
        /// Retrieves email body based on XSL transform of XML event
        /// </summary>
        internal static string Body (string eventXml)
        {
            var transform = new XslCompiledTransform ();
            transform.Load (HttpContext.Current.Server.MapPath (ConfigurationManager.AppSettings ["BodyTransform"]));

            var input = new XPathDocument (new StringReader (eventXml));
            var body = new MemoryStream ();

            transform.Transform (input, new XmlTextWriter (body, Encoding.UTF8));

            return Encoding.UTF8.GetString (body.ToArray ());
        }
    }
}